package top.appx.easysql;

import top.appx.easysql.ex.EasySqlException;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * Created by 燎火 on 2015/10/11.
 */
public class MySqlDatabase extends BaseDatabase {
    static{
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    private String url;
    private String user;
    private String password;
    public MySqlDatabase(String url,String user,String password) {
        this.url = url;
        this.user = user;
        this.password = password;
    }

    @Override
    protected Connection createConnection() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            throw new EasySqlException(e);
        }
        return conn;
    }

    @Override
    protected void closeConnection() {
        if(connection !=null){
            try {
                connection.close();
            } catch (SQLException e) {
                throw new EasySqlException(e);
            }
        }
    }
    @Override
    protected String AutoIncreSql(String... tablename){
        return "select last_insert_id()";
    }

    @Override
    protected String packMax(String sql,int start, int max) {
        if(start == 0 &&  max !=0 ){
            return sql+" limit "+max;
        }
        if(start != 0 && max == 0){
            return sql + " limit "+start+",9999999999";
        }

        if(start!=0 && max!=0){
            return sql + " limit "+start+","+max;
        }
        return sql;
    }


}
